package com.my.util.xmlPaser;

import com.my.entity.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.StaxDriver;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wt on 2017/11/29.
 */
public class XStreamTest {
    public static void main(String[] args) throws Exception {
        toXml();
    }

    private static void toXml() throws Exception {
        List<Url> list = new ArrayList<>();

        for (int i = 1; i < 3; i++) {
            Data data = new Data();
            data.setName("apple"+i);
            data.setOuterID(""+i);

            Url url = new Url();
            url.setLoc("https://item.m.jd.com/product/"+i+".html");
            url.setData(data);


            list.add(url);

        }

        XStream xstream = new XStream(new StaxDriver());
//        XStream xstream = new XStream();
        xstream.alias("urlset", List.class);
        xstream.alias("url", Url.class);
        xstream.alias("data", Data.class);

        String xmlHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
//        String xmlHead = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>&gt; \n";
        String s = xmlHead + xstream.toXML(list);
        try {
            FileOutputStream fs = new FileOutputStream("feed_xstream.xml");
//            xstream.toXML(s, fs);
            xstream.toXML(list, fs);
            fs.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }



}
